package com.company.OfferGuide;

import com.company.TreeNode;

public class 序列化二叉树 {


    // Encodes a tree to a single string.
    public String serialize(TreeNode root) {
        if (root == null) {
            return "#";
        }
        return root.val + "," + serialize(root.left) + "," + serialize(root.right);
    }
    int index = -1;
    // Decodes your encoded data to tree.
    public TreeNode deserialize(String data) {
        String[] split = data.split(",");
        index++;
        TreeNode node = null;
        if (!split[index].equals("#")) {
            node = new TreeNode(Integer.valueOf(split[index]));
            node.left = deserialize(data);
            node.right = deserialize(data);
        }

        return node;
    }

}
